package com.woniu.defecttracking.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniu.defecttracking.annotation.AddNum;
import com.woniu.defecttracking.commens.ComUtil;
import com.woniu.defecttracking.dto.RuleDto;
import com.woniu.defecttracking.dto.RulePageDto;
import com.woniu.defecttracking.entity.Rule;
import com.woniu.defecttracking.exception.RuleException;
import com.woniu.defecttracking.mapper.RuleMapper;
import com.woniu.defecttracking.param.AddRuleParam;
import com.woniu.defecttracking.param.FindRuleParam;
import com.woniu.defecttracking.param.UpdateRuleParam;
import com.woniu.defecttracking.service.RuleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  rule的service层实现类
 * </p>
 *
 * @author tiangle
 * @since 2021-12-31
 */
@Service
public class RuleServiceImpl extends ServiceImpl<RuleMapper, Rule> implements RuleService {

    /**
    * @作者: 倪西
    * @描述：获取所有未删除的rule
    * @时间：2022/1/6 15:09
    * @参数：[]
    * @返回：java.util.List<com.woniu.defecttracking.dto.RuleDto>
    **/
    @Override
    public List<RuleDto> getRules() {
        List<Rule> rules = list(new QueryWrapper<Rule>().eq("rule_isdel",0));
        List<RuleDto> ruleDtos = ComUtil.copyList(rules, RuleDto.class);
        return ruleDtos;
    }

    @Override
    public List<RuleDto> getRulesByIds(List<Integer> ids) throws RuleException {
//        List<Rule> rules = listByIds(ids);
        if (ids.size()<=0){
            throw new RuleException("对应的ruleIds不存在",2076);
        }
        List<Rule> rules=list(new QueryWrapper<Rule>().eq("rule_isdel",0).in("rule_id",ids));
        if (ids.size()!=rules.size()){
            throw new RuleException("有rule不存在",2075);
        }
        return ComUtil.copyList(rules,RuleDto.class);

    }

    @Override
    public RulePageDto findRule(FindRuleParam findRuleParam){
        Page<Rule> rulePage=null;
        if (findRuleParam.getKeyword()==null||findRuleParam.getKeyword().trim().equals("")){
            rulePage = page(new Page<Rule>(findRuleParam.getPageIndex(),findRuleParam.getPageSize()),
                    new QueryWrapper<Rule>().eq("rule_isdel",0));
        }else{
            rulePage = page(new Page<Rule>(findRuleParam.getPageIndex(),findRuleParam.getPageSize()),
                    new QueryWrapper<Rule>().eq("rule_isdel",0).like("rule_name", findRuleParam.getKeyword()));
        }
        RulePageDto rulePageDto = new RulePageDto();
        rulePageDto.setPageIndex(rulePage.getCurrent());
        rulePageDto.setTotal(rulePage.getTotal());
        List<RuleDto> ruleDtos = ComUtil.copyList(rulePage.getRecords(), RuleDto.class);
        rulePageDto.setRuleDtoList(ruleDtos);
        return rulePageDto;
    }
    @AddNum("ruleNum")
    @Override
    public RuleDto saveRule(AddRuleParam addRuleParam) throws RuleException {
        if (null==addRuleParam.getRuleNum()||addRuleParam.getRuleNum().trim().equals("")){
            throw new RuleException("新增规则的参数异常",2071);
        }
        if (null==addRuleParam.getRuleName()||addRuleParam.getRuleName().trim().equals("")){
            throw new RuleException("新增规则的参数异常",2071);
        }
        Rule rule = ComUtil.copyProperties(addRuleParam, Rule.class);
        rule.setRuleStatus(0);//设置状态默认为0，为禁用状态
        rule.setRuleIsdel(0);//设置状态默认为0，为未删除状态
        save(rule);
        return ComUtil.copyProperties(rule,RuleDto.class);
    }

    @Override
    public Boolean changeRule(Integer id) throws RuleException {
        Rule rule = getById(id);
        if (null==rule){
            throw new RuleException("rule不存在",2072);
        }
        if (rule.getRuleIsdel()==1){
            throw new RuleException("rule已删除,不可修改",2073);
        }
        if (rule.getRuleStatus()==0){
            rule.setRuleStatus(1);
        }else {
            rule.setRuleStatus(0);
        }
        return saveOrUpdate(rule);
    }

    @Override
    public Boolean updateRule(UpdateRuleParam updateRuleParam) throws RuleException {
        if (updateRuleParam.getRuleName()==null||updateRuleParam.getRuleNum().trim().equals("")){
            throw new RuleException("修改规则的参数异常",2073);
        }
        if (updateRuleParam.getRuleNum()==null||updateRuleParam.getRuleName().trim().equals("")){
            throw new RuleException("修改规则的参数异常",2073);
        }
        Rule rule = ComUtil.copyProperties(updateRuleParam, Rule.class);
        return saveOrUpdate(rule);
    }

    @Override
    public Boolean removeRule(List<Integer> ids) {
        List<Rule> rules = listByIds(ids);
        if (rules.size()==ids.size()){
//            rules.forEach(item->item.setRuleStatus(1));
            for (Rule rule:rules){
                rule.setRuleIsdel(1);
            }
            return saveOrUpdateBatch(rules);
        }else {
            return false;
        }
    }
}
